package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：qcq
 * @date ：Created in 2020/12/6 14:22
 * @description：规格组web层
 */
@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    //根据商品分类id，查询规格组的集合
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(specGroupService.querySpecGroupByCategory(id));
    }

    //根据分类id查询规格组及组内参数
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupAndParamByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(specGroupService.querySpecGroupAndParamByCategoryId(id));
    }

    /*todo*/
    //根据条件查询规格参数集合
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParam(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching) {

        return ResponseEntity.ok(specParamService.querySpecParam(categoryId, groupId, searching));
    }

    @PostMapping("/group")
    public ResponseEntity addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        // 新增规格组
        specGroupService.addSpecGroup(specGroupDTO);
        // 返回结果
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/group")
    public ResponseEntity updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        specGroupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/param")
    public ResponseEntity updateSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.updateSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PostMapping("/param")
    public ResponseEntity addSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.addSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
